
<!DOCTYPE HTML>
<html lang="" >
    <head>
        <meta charset="UTF-8">
        <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
        <title>2.3 模型字段类型 · GitBook</title>
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="description" content="">
        <meta name="generator" content="GitBook 3.2.2">
        
        
        
    
    <link rel="stylesheet" href="../gitbook/style.css">

    
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-highlight/website.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-search/search.css">
                
            
                
                <link rel="stylesheet" href="../gitbook/gitbook-plugin-fontsettings/website.css">
                
            
        

    

    
        
    
        
    
        
    
        
    
        
    
        
    

        
    
    
    <meta name="HandheldFriendly" content="true"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="apple-mobile-web-app-capable" content="yes">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <link rel="apple-touch-icon-precomposed" sizes="152x152" href="../gitbook/images/apple-touch-icon-precomposed-152.png">
    <link rel="shortcut icon" href="../gitbook/images/favicon.ico" type="image/x-icon">

    
    <link rel="next" href="6.导入与配置.html" />
    
    
    <link rel="prev" href="4.定义一个模型.html" />
    

    </head>
    <body>
        
<div class="book">
    <div class="book-summary">
        
            
<div id="book-search-input" role="search">
    <input type="text" placeholder="Type to search" />
</div>

            
                <nav role="navigation">
                


<ul class="summary">
    
    

    

    
        
        
    
        <li class="chapter " data-level="1.1" data-path="../">
            
                <a href="../">
            
                    
                    Introduction
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.2" data-path="1.写在前面的话.html">
            
                <a href="1.写在前面的话.html">
            
                    
                    写在前面的话
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.3" data-path="2.搭建环境.html">
            
                <a href="2.搭建环境.html">
            
                    
                    第一章 搭建环境
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4" >
            
                <span>
            
                    
                    第二章 模型定义
            
                </span>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.4.1" data-path="3.sequelize 实例变量.html">
            
                <a href="3.sequelize 实例变量.html">
            
                    
                    2.1 实例变量方法
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.2" data-path="4.定义一个模型.html">
            
                <a href="4.定义一个模型.html">
            
                    
                    2.2 定义一个模型
            
                </a>
            

            
        </li>
    
        <li class="chapter active" data-level="1.4.3" data-path="5.模型字段类型.html">
            
                <a href="5.模型字段类型.html">
            
                    
                    2.3 模型字段类型
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.4.4" data-path="6.导入与配置.html">
            
                <a href="6.导入与配置.html">
            
                    
                    2.3 导入与配置
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.5" data-path="3模型关系.html">
            
                <a href="3模型关系.html">
            
                    
                    第三章 模型关系
            
                </a>
            

            
            <ul class="articles">
                
    
        <li class="chapter " data-level="1.5.1" data-path="3.1属于.html">
            
                <a href="3.1属于.html">
            
                    
                    3.1 属于
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.2" data-path="3.2包含.html">
            
                <a href="3.2包含.html">
            
                    
                    3.2 包含
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.3" data-path="3.3一对多.html">
            
                <a href="3.3一对多.html">
            
                    
                    3.3 一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.4" data-path="3.4多对多.html">
            
                <a href="3.4多对多.html">
            
                    
                    3.4 多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.5" data-path="3.5多态一对多.html">
            
                <a href="3.5多态一对多.html">
            
                    
                    3.5 多态一对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.6" data-path="3.6多态多对多.html">
            
                <a href="3.6多态多对多.html">
            
                    
                    3.6 多态多对多
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.5.7" data-path="3.7关系嵌套.html">
            
                <a href="3.7关系嵌套.html">
            
                    
                    3.7 关系嵌套
            
                </a>
            

            
        </li>
    

            </ul>
            
        </li>
    
        <li class="chapter " data-level="1.6" data-path="4模型查询.html">
            
                <a href="4模型查询.html">
            
                    
                    第四章 模型查询
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.7" data-path="5查询范围.html">
            
                <a href="5查询范围.html">
            
                    
                    第五章 查询范围 Scope (预定义查询条件)
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.8" data-path="6生命周期函数.html">
            
                <a href="6生命周期函数.html">
            
                    
                    第六章 生命周期函数 Hook
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.9" data-path="7migrate.html">
            
                <a href="7migrate.html">
            
                    
                    第七章 数据库迁移 Migrate
            
                </a>
            

            
        </li>
    
        <li class="chapter " data-level="1.10" data-path="8tabel2model.html">
            
                <a href="8tabel2model.html">
            
                    
                    第八章 根据 Table 生成 Model
            
                </a>
            

            
        </li>
    

    

    <li class="divider"></li>

    <li>
        <a href="https://www.gitbook.com" target="blank" class="gitbook-link">
            Published with GitBook
        </a>
    </li>
</ul>


                </nav>
            
        
    </div>

    <div class="book-body">
        
            <div class="body-inner">
                
                    

<div class="book-header" role="navigation">
    

    <!-- Title -->
    <h1>
        <i class="fa fa-circle-o-notch fa-spin"></i>
        <a href=".." >2.3 模型字段类型</a>
    </h1>
</div>




                    <div class="page-wrapper" tabindex="-1" role="main">
                        <div class="page-inner">
                            
<div id="book-search-results">
    <div class="search-noresults">
    
                                <section class="normal markdown-section">
                                
                                <h1 id="&#x6A21;&#x578B;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;">&#x6A21;&#x578B;&#x5B57;&#x6BB5;&#x7C7B;&#x578B;</h1>
<p>&#x5728;&#x4E0A;&#x4E00;&#x8282;&#x4E2D;&#xFF0C;&#x5B57;&#x6BB5;&#x7684;&#x914D;&#x7F6E;&#x662F;&#x8FD9;&#x6837;&#x7684;&#x3002;<code>S</code>&#x5219;&#x662F;&#x5F15;&#x5165;&#x7684;<code>Sequelize</code>&#xFF0C;&#x4E3A;&#x4E86;&#x7F16;&#x5199;&#x65B9;&#x4FBF;&#xFF0C;&#x91CD;&#x547D;&#x540D;&#x4E86;&#x4E00;&#x4E0B;&#x3002;</p>
<pre><code class="lang-ts">{
    email: S.STRING,
    name: S.STRING
}
</code></pre>
<p>&#x5728; define &#x51FD;&#x6570;&#x7684;&#x58F0;&#x660E;&#x91CC;&#x9762;&#xFF0C;&#x9700;&#x8981;&#x4F20;&#x5165;&#x7684;&#x5B57;&#x6BB5;&#x914D;&#x7F6E;&#x9879;&#x5176;&#x5B9E;&#x662F; <code>DefineAttributes</code> &#x7C7B;&#x578B;&#x3002;</p>
<pre><code class="lang-ts">[name: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">string</span> | DataTypeAbstract | DefineAttributeColumnOptions;
</code></pre>
<p>&#x5B9A;&#x4E49;&#x5982;&#x4E0A;&#xFF0C;&#x8FD9;&#x8868;&#x793A;&#x8FD9;&#x5BF9;&#x8C61;&#x91CC;&#x9762;&#x7684; key &#x5FC5;&#x987B;&#x662F;&#x4E00;&#x4E2A; string&#xFF0C;&#x800C; value &#x5219;&#x662F; string &#x3001;DataTypeAbstract &#x3001;DefineAttributeColumnOptions &#x4E09;&#x8005;&#x5176;&#x4E00;&#x5373;&#x53EF;&#x3002;</p>
<p><strong>1.DataTypeAbstract</strong></p>
<p><code>DataTypeAbstract</code> &#x53EA;&#x662F;&#x4E00;&#x4E2A;&#x7236;&#x63A5;&#x53E3;&#xFF0C;&#x800C; DataTypeString &#x3001;DataTypeBigInt &#x3001;DataTypeFloat &#x5219;&#x90FD;&#x662F;&#x7EE7;&#x627F;&#x4E86;&#x8FD9;&#x4E2A;&#x7236;&#x63A5;&#x53E3;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4;&#x6211;&#x4EEC;&#x4F20;&#x5165; DataTypeString &#x7C7B;&#x578B;&#x4E5F;&#x662F;&#x53EF;&#x4EE5;&#x7684;&#xFF0C;&#x56E0;&#x4E3A;&#x5728;&#x9762;&#x5411;&#x5BF9;&#x8C61;&#x7684;&#x7EE7;&#x627F;&#x4E2D;&#xFF0C;&#x5B50;&#x7C7B;&#x662F;&#x53EF;&#x4EE5;&#x8D4B;&#x503C;&#x7ED9;&#x7236;&#x7C7B;&#x7684;&#x3002;</p>
<p>&#x800C;&#x6211;&#x4EEC;&#x7684; DataTypes &#x7684;&#x5B9A;&#x4E49;&#x662F;&#x8FD9;&#x6837;&#x7684;&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x6211;&#x4EEC;&#x6240;&#x6709;&#x652F;&#x6301;&#x7684;&#x7C7B;&#x578B;&#xFF0C;&#x4F46;&#x662F;&#x6570;&#x636E;&#x5E93;&#x4E4B;&#x95F4;&#x662F;&#x5B58;&#x5728;&#x5DEE;&#x5F02;&#x7684;&#xFF0C;&#x50CF; MYSQL &#x5C31;&#x4E0D;&#x652F;&#x6301; JSON &#x7C7B;&#x3001;Array &#x7C7B;&#x7B49;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> DataTypes {
   ABSTRACT: DataTypeAbstract;
   STRING: DataTypeString;
   CHAR: DataTypeChar;
   TEXT: DataTypeText;
   NUMBER: DataTypeNumber;
   INTEGER: DataTypeInteger;
   BIGINT: DataTypeBigInt;
   FLOAT: DataTypeFloat;
   TIME: DataTypeTime;
   DATE: DataTypeDate;
   DATEONLY: DataTypeDateOnly;
   BOOLEAN: DataTypeBoolean;
   NOW: DataTypeNow;
   BLOB: DataTypeBlob;
   DECIMAL: DataTypeDecimal;
   NUMERIC: DataTypeDecimal;
   UUID: DataTypeUUID;
   UUIDV1: DataTypeUUIDv1;
   UUIDV4: DataTypeUUIDv4;
   HSTORE: DataTypeHStore;
   <span class="hljs-built_in">JSON</span>: DataTypeJSONType;
   JSONB: DataTypeJSONB;
   VIRTUAL: DataTypeVirtual;
   ARRAY: DataTypeArray;
   NONE: DataTypeVirtual;
   ENUM: DataTypeEnum;
   RANGE: DataTypeRange;
   REAL: DataTypeReal;
   DOUBLE: DataTypeDouble;
   <span class="hljs-string">&quot;DOUBLE PRECISION&quot;</span>: DataTypeDouble;
   GEOMETRY: DataTypeGeometry;
}
</code></pre>
<p>&#x5E76;&#x4E14; <code>interface SequelizeStatic extends SequelizeStaticAndInstance, DataTypes</code>&#xFF0C;&#x4E5F;&#x5C31;&#x662F;&#x8BF4; <code>SequelizeStatic</code> &#x662F;&#x7EE7;&#x627F;&#x4E86; DataTypes &#x7684;&#xFF0C;&#x6240;&#x4EE5;&#x6211;&#x4E48;&#x53EF;&#x4EE5;&#x901A;&#x8FC7; <code>Sequelize.INTEGER</code>&#x6765;&#x8BBF;&#x95EE; DataTypes &#x7684;&#x4E00;&#x4E9B;&#x53D8;&#x91CF;&#x3002;</p>
<p><strong>2.DefineAttributeColumnOptions</strong></p>
<p>&#x9996;&#x5148;&#x7EE7;&#x627F;&#x4E86; <code>ColumnOptions</code> &#x63A5;&#x53E3;&#xFF0C;&#x90FD;&#x662F;&#x4E00;&#x4E9B;&#x53EF;&#x9009;&#x5C5E;&#x6027;&#xFF0C;&#x4E86;&#x89E3;&#x6570;&#x636E;&#x5E93;&#x77E5;&#x8BC6;&#x7684;&#x540C;&#x5B66;&#xFF0C;&#x5BF9;&#x5927;&#x90E8;&#x5206;&#x914D;&#x7F6E;&#x9879;&#x5E94;&#x8BE5;&#x90FD;&#x80FD;&#x731C;&#x5230;&#x662F;&#x5E72;&#x4EC0;&#x4E48;&#x7528;&#x7684;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> ColumnOptions {
   allowNull?: <span class="hljs-built_in">boolean</span>;
   field?: <span class="hljs-built_in">string</span>;
   defaultValue?: <span class="hljs-built_in">any</span>;
}
</code></pre>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> DefineAttributeColumnOptions <span class="hljs-keyword">extends</span> ColumnOptions {
   <span class="hljs-keyword">type</span>: <span class="hljs-built_in">string</span> | DataTypeAbstract;
   unique?: <span class="hljs-built_in">boolean</span> | <span class="hljs-built_in">string</span> | { name: <span class="hljs-built_in">string</span>, msg: <span class="hljs-built_in">string</span> };
   primaryKey?: <span class="hljs-built_in">boolean</span>;
   autoIncrement?: <span class="hljs-built_in">boolean</span>;
   comment?: <span class="hljs-built_in">string</span>;
   references?: DefineAttributeColumnReferencesOptions;
   onUpdate?: <span class="hljs-built_in">string</span>;
   onDelete?: <span class="hljs-built_in">string</span>;
   <span class="hljs-keyword">get</span>?: () =&gt; <span class="hljs-built_in">any</span>;
   <span class="hljs-keyword">set</span>?: (val: <span class="hljs-built_in">any</span>) =&gt; <span class="hljs-built_in">void</span>;
   validate?: DefineValidateOptions;
   values?: <span class="hljs-built_in">string</span>[];
}
</code></pre>
<p>&#x540C;&#x65F6;&#x6709;&#x8FD9;&#x4E2A;<code>validate</code>&#x8FD9;&#x4E2A;&#x9009;&#x9879;&#xFF0C;&#x5176;&#x5B9E;&#x662F;&#x652F;&#x6301;&#x9A8C;&#x8BC1;&#x7684;&#x3002;&#x4E5F;&#x5C31;&#x662F;&#x7528;&#x4E86;&#x4E00;&#x4E2A;<code>validator</code>&#x7684;&#x5E93;&#x3002;&#x5B83;&#x6240;&#x652F;&#x6301;&#x7684;&#x914D;&#x7F6E;&#x9879;&#x5982;&#x4E0B;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> DefineValidateOptions {
   is?: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">RegExp</span>&gt; | <span class="hljs-built_in">RegExp</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">RegExp</span>&gt; | <span class="hljs-built_in">RegExp</span> };
   not?: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">RegExp</span>&gt; | <span class="hljs-built_in">RegExp</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span> | <span class="hljs-built_in">Array</span>&lt;<span class="hljs-built_in">string</span> | <span class="hljs-built_in">RegExp</span>&gt; | <span class="hljs-built_in">RegExp</span> };
   isEmail?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isUrl?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isIP?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isIPv4?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isIPv6?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isAlpha?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isAlphanumeric?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isNumeric?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isInt?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isFloat?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isDecimal?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isLowercase?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isUppercase?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   notNull?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   isNull?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   notEmpty?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span> };
   equals?: <span class="hljs-built_in">string</span> | { msg: <span class="hljs-built_in">string</span> };
   contains?: <span class="hljs-built_in">string</span> | { msg: <span class="hljs-built_in">string</span> };
   notIn?: <span class="hljs-built_in">string</span>[][] | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span>[][] };
   isIn?: <span class="hljs-built_in">string</span>[][] | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span>[][] };
   notContains?: <span class="hljs-built_in">string</span>[] | <span class="hljs-built_in">string</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span>[] | <span class="hljs-built_in">string</span> };
   len?: [<span class="hljs-built_in">number</span>, <span class="hljs-built_in">number</span>] | { msg: <span class="hljs-built_in">string</span>, args: [<span class="hljs-built_in">number</span>, <span class="hljs-built_in">number</span>] };
   isUUID?: <span class="hljs-built_in">number</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">number</span> };
   isDate?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">boolean</span> };
   isAfter?: <span class="hljs-built_in">string</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span> };
   isBefore?: <span class="hljs-built_in">string</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">string</span> };
   max?: <span class="hljs-built_in">number</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">number</span> };
   min?: <span class="hljs-built_in">number</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">number</span> };
   isArray?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">boolean</span> };
   isCreditCard?: <span class="hljs-built_in">boolean</span> | { msg: <span class="hljs-built_in">string</span>, args: <span class="hljs-built_in">boolean</span> };  
   [name: <span class="hljs-built_in">string</span>]: <span class="hljs-built_in">any</span>;
}
</code></pre>
<h4 id="&#x5C0F;&#x8354;&#x679D;">&#x5C0F;&#x8354;&#x679D;</h4>
<pre><code class="lang-ts"><span class="hljs-keyword">interface</span> BookAttributes {
  <span class="hljs-comment">// status: &quot;inSale&quot; | &quot;noSale&quot;;</span>
  status: <span class="hljs-built_in">any</span>;
  description: <span class="hljs-built_in">string</span>;
  title: <span class="hljs-built_in">string</span>;
  author: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">interface</span> BookInstance <span class="hljs-keyword">extends</span> Sequelize.Instance&lt;BookAttributes&gt; {
  id: <span class="hljs-built_in">number</span>;
  createdAt: <span class="hljs-built_in">Date</span>;
  updatedAt: <span class="hljs-built_in">Date</span>;

  <span class="hljs-comment">// status: &quot;inSale&quot; | &quot;noSale&quot;;</span>
  status: <span class="hljs-built_in">any</span>;
  description: <span class="hljs-built_in">string</span>;
  title: <span class="hljs-built_in">string</span>;
  author: <span class="hljs-built_in">string</span>;
}

<span class="hljs-keyword">const</span> Book = sequelize.define&lt;BookInstance, BookAttributes&gt;(<span class="hljs-string">&apos;Book&apos;</span>, {
  id: {
    <span class="hljs-keyword">type</span>: S.INTEGER,
    autoIncrement: <span class="hljs-literal">true</span>,
    primaryKey: <span class="hljs-literal">true</span>,
    unique: <span class="hljs-literal">true</span>
  },
  description: S.TEXT,
  status: {
    <span class="hljs-keyword">type</span>: S.ENUM,
    values: [<span class="hljs-string">&apos;inSale&apos;</span>, <span class="hljs-string">&apos;noSale&apos;</span>],
    validate: {
      isIn: {
        args: [[<span class="hljs-string">&apos;inSale&apos;</span>, <span class="hljs-string">&apos;noSale&apos;</span>]],
        msg: <span class="hljs-string">&quot;status field must be inSale or noSale&quot;</span>
      }
    }
  },
  title:{
    <span class="hljs-keyword">type</span>: S.STRING,
    allowNull: <span class="hljs-literal">false</span>,
    <span class="hljs-keyword">get</span>(<span class="hljs-keyword">this</span>: BookInstance) {
      <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.getDataValue(<span class="hljs-string">&apos;author&apos;</span>) + <span class="hljs-string">&apos; - &apos;</span> + <span class="hljs-keyword">this</span>.getDataValue(<span class="hljs-string">&apos;title&apos;</span>);
    }
  },
  author: {
    <span class="hljs-keyword">type</span>: S.STRING,
    allowNull: <span class="hljs-literal">false</span>,
    <span class="hljs-keyword">set</span>(val: <span class="hljs-built_in">string</span>){
      <span class="hljs-keyword">this</span>.setDataValue(<span class="hljs-string">&apos;author&apos;</span>, val.toLowerCase());
    }
  },
  userId:{ <span class="hljs-comment">// User &#x8868;&#x7684;&#x5916;&#x952E;</span>
    <span class="hljs-keyword">type</span>: S.INTEGER,
    references: {
      model: User
    }
  }
},{
    comment: <span class="hljs-string">&quot;&#x56FE;&#x4E66;&#x8868;&quot;</span>, <span class="hljs-comment">// &#x8868;&#x6CE8;&#x91CA;</span>
    indexes: [  <span class="hljs-comment">// &#x8868;&#x7D22;&#x5F15;</span>
      {
        fields: [<span class="hljs-string">&apos;id&apos;</span>]
      }
    ]
})
</code></pre>
<p><code>// status: &quot;inSale&quot; | &quot;noSale&quot;;</code> &#x6CE8;&#x91CA;&#x6389;&#x8FD9;&#x4E2A;&#x7684;&#x539F;&#x56E0;&#x662F;&#x4E3A;&#x4E86;&#x907F;&#x514D;ts &#x7F16;&#x8BD1;&#x65F6;&#x62A5;&#x9519;&#xFF0C;&#x6211;&#x4EEC;&#x5148;&#x7ED9;&#x5B83;&#x4E00;&#x4E2A; any &#x7C7B;&#x578B;&#xFF0C;&#x6765;&#x9A8C;&#x8BC1;&#x4E00;&#x4E0B; validate &#x65B9;&#x6CD5;&#x662F;&#x5426;&#x771F;&#x6B63;&#x7684;&#x8D77;&#x4E86;&#x9A8C;&#x8BC1;&#x4F5C;&#x7528;&#x3002; </p>
<h4 id="main-&#x65B9;&#x6CD5;">main &#x65B9;&#x6CD5;</h4>
<pre><code class="lang-ts">async <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">main</span>(<span class="hljs-params"></span>) </span>{
  <span class="hljs-keyword">try</span> {
    <span class="hljs-comment">// await Book.sync();</span>
    <span class="hljs-comment">// await User.sync();</span>

    Book.beforeValidate(() =&gt; {
      <span class="hljs-built_in">console</span>.log(<span class="hljs-string">&quot;&#x9A8C;&#x8BC1;&#x4E4B;&#x524D;&quot;</span>);
    });

    <span class="hljs-keyword">const</span> book = Book.build({ status: <span class="hljs-string">&quot;onSale&quot;</span>, author: <span class="hljs-string">&quot;yugo&quot;</span>, description: <span class="hljs-string">&quot;lean ts&quot;</span>, title: <span class="hljs-string">&quot;typescript&quot;</span> });
    await book.validate()

  }<span class="hljs-keyword">catch</span>(e){
    <span class="hljs-comment">// console.log(e)</span>
    <span class="hljs-keyword">if</span> (e <span class="hljs-keyword">instanceof</span> Sequelize.ValidationError) {
      <span class="hljs-built_in">console</span>.log(e.message);
    }
  }
  process.exit(<span class="hljs-number">0</span>)
}
</code></pre>
<p>&#x8FD0;&#x884C;&#x53EF;&#x5F97;&#x5230;&#x5982;&#x4E0B;&#x7ED3;&#x679C;&#xFF0C;&#x8BF4;&#x660E;&#x9A8C;&#x8BC1;&#x9519;&#x8BEF;&#x662F;&#x88AB; catch &#x6355;&#x83B7;&#x5230;&#x4E86;&#x7684;&#xFF0C;&#x800C;&#x4E14;&#x4E5F;&#x6253;&#x5370;&#x4E86;&#x51FA;&#x6765;&#x3002;</p>
<pre><code class="lang-bash"><span class="hljs-comment"># Yugo @ Tractor in ~/Desktop/nl-sequelize/code/chapter2 on git:master x [13:19:00]</span>
$ ts-node index.ts
&#x9A8C;&#x8BC1;&#x4E4B;&#x524D;
Validation error: status field must be <span class="hljs-keyword">in</span>Sale or noSale
</code></pre>
<p>&#x5F53;&#x628A;<code>book.validate</code>&#x6539;&#x6210;<code>book.save</code>&#x540C;&#x6837;&#x4E5F;&#x4F1A;&#x81EA;&#x52A8;&#x8C03;&#x7528;&#x9A8C;&#x8BC1;&#x65B9;&#x6CD5;&#x3002;&#x63A5;&#x4E0B;&#x6765;&#xFF0C;&#x628A;&#x524D;&#x9762;&#x7684;&#x63A5;&#x53E3;&#x6539;&#x56DE;&#x6765;&#xFF0C;&#x518D;&#x6765;&#x8BD5;&#x4E00;&#x8BD5;&#x6B63;&#x786E;&#x7684;&#x3002;</p>
<pre><code class="lang-ts"><span class="hljs-keyword">const</span> book = Book.build({ status: <span class="hljs-string">&quot;inSale&quot;</span>, author: <span class="hljs-string">&quot;yugo&quot;</span>, description: <span class="hljs-string">&quot;lean ts&quot;</span>, title: <span class="hljs-string">&quot;typescript&quot;</span> });
await book.save()
</code></pre>
<pre><code class="lang-bash"><span class="hljs-comment"># Yugo @ Tractor in ~/Desktop/nl-sequelize/code/chapter2 on git:master x [13:27:55]</span>
$ ts-node index.ts
&#x9A8C;&#x8BC1;&#x4E4B;&#x524D;
Executing (default): INSERT INTO `Books` (`id`,`description`,`status`,`title`,`author`,`createdAt`,`updatedAt`) VALUES (DEFAULT,<span class="hljs-string">&apos;lean ts&apos;</span>,<span class="hljs-string">&apos;inSale&apos;</span>,<span class="hljs-string">&apos;typescript&apos;</span>,<span class="hljs-string">&apos;yugo&apos;</span>,<span class="hljs-string">&apos;2017-06-19 05:29:35&apos;</span>,<span class="hljs-string">&apos;2017-06-19 05:29:35&apos;</span>);
</code></pre>
<p><strong>&#x5C0F;&#x63D0;&#x793A;&#xFF1A;</strong> &#x6709;&#x7684;&#x65F6;&#x5019;&#x53EF;&#x80FD;&#x63D0;&#x793A;&#x5E76;&#x6CA1;&#x6709;&#x544A;&#x8BC9;&#x4F60;&#x53EF;&#x4EE5;&#x8C03;&#x7528;&#x54EA;&#x4E9B;&#x65B9;&#x6CD5;&#xFF0C;&#x5927;&#x591A;&#x6570;&#x662F;&#x56E0;&#x4E3A;&#x4EE3;&#x7801;&#x7F16;&#x8F91;&#x5668;&#x7684;&#x95EE;&#x9898;&#xFF0C;&#x76F4;&#x63A5;&#x53BB;&#x67E5;&#x770B;d.ts&#xFF0C;&#x81EA;&#x5DF1;&#x627E;&#x4E00;&#x4E0B;&#x5373;&#x53EF;&#x3002;</p>

                                
                                </section>
                            
    </div>
    <div class="search-results">
        <div class="has-results">
            
            <h1 class="search-results-title"><span class='search-results-count'></span> results matching "<span class='search-query'></span>"</h1>
            <ul class="search-results-list"></ul>
            
        </div>
        <div class="no-results">
            
            <h1 class="search-results-title">No results matching "<span class='search-query'></span>"</h1>
            
        </div>
    </div>
</div>

                        </div>
                    </div>
                
            </div>

            
                
                <a href="4.定义一个模型.html" class="navigation navigation-prev " aria-label="Previous page: 2.2 定义一个模型">
                    <i class="fa fa-angle-left"></i>
                </a>
                
                
                <a href="6.导入与配置.html" class="navigation navigation-next " aria-label="Next page: 2.3 导入与配置">
                    <i class="fa fa-angle-right"></i>
                </a>
                
            
        
    </div>

    <script>
        var gitbook = gitbook || [];
        gitbook.push(function() {
            gitbook.page.hasChanged({"page":{"title":"2.3 模型字段类型","level":"1.4.3","depth":2,"next":{"title":"2.3 导入与配置","level":"1.4.4","depth":2,"path":"document/6.导入与配置.md","ref":"./document/6.导入与配置.md","articles":[]},"previous":{"title":"2.2 定义一个模型","level":"1.4.2","depth":2,"path":"document/4.定义一个模型.md","ref":"./document/4.定义一个模型.md","articles":[]},"dir":"ltr"},"config":{"gitbook":"*","theme":"default","variables":{},"plugins":["livereload"],"pluginsConfig":{"livereload":{},"highlight":{},"search":{},"lunr":{"maxIndexSize":1000000,"ignoreSpecialCharacters":false},"sharing":{"facebook":true,"twitter":true,"google":false,"weibo":false,"instapaper":false,"vk":false,"all":["facebook","google","twitter","weibo","instapaper"]},"fontsettings":{"theme":"white","family":"sans","size":2},"theme-default":{"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"},"showLevel":false}},"structure":{"langs":"LANGS.md","readme":"README.md","glossary":"GLOSSARY.md","summary":"SUMMARY.md"},"pdf":{"pageNumbers":true,"fontSize":12,"fontFamily":"Arial","paperSize":"a4","chapterMark":"pagebreak","pageBreaksBefore":"/","margin":{"right":62,"left":62,"top":56,"bottom":56}},"styles":{"website":"styles/website.css","pdf":"styles/pdf.css","epub":"styles/epub.css","mobi":"styles/mobi.css","ebook":"styles/ebook.css","print":"styles/print.css"}},"file":{"path":"document/5.模型字段类型.md","mtime":"2017-06-19T06:05:18.000Z","type":"markdown"},"gitbook":{"version":"3.2.2","time":"2017-06-28T08:29:37.466Z"},"basePath":"..","book":{"language":""}});
        });
    </script>
</div>

        
    <script src="../gitbook/gitbook.js"></script>
    <script src="../gitbook/theme.js"></script>
    
        
        <script src="../gitbook/gitbook-plugin-livereload/plugin.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search-engine.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-search/search.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/lunr.min.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-lunr/search-lunr.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-sharing/buttons.js"></script>
        
    
        
        <script src="../gitbook/gitbook-plugin-fontsettings/fontsettings.js"></script>
        
    

    </body>
</html>

